package tournament.volleyball;

import org.codehaus.jackson.annotate.*;


public class Game{
	private static int nextId = 0;
	private int id;
	
	private int scoreTeam1;
	private int scoreTeam2;
	
	private transient Rivalry parent;
	
	// Empty constructor for GSON
	private Game() {
	}
	
	// This constructor used to add a new game to a rivalry
	// and initializes it to the rivalry list
	Game(Game copy) {
		this.id = nextId;
		nextId += 1;
		copy.parent.addGame(this);
		
		initialize();
	}
	
	// This constructor is used by the Rivalry constructor to add games
	// to the rivalry list
	Game(Rivalry init) {
		this.id = nextId;
		nextId += 1;
		this.parent = init;
		
		initialize();
	}
	
	// This constructor is for the laziness in us if we just want to create a single game
	// and not worry about the game list.
	Game(Team team1, Team team2) {
		this.id = nextId;
		Game.nextId += 1;
		this.parent = new Rivalry(team1, team2, this);
		
		initialize();
	}
	
	private void initialize() {
		this.scoreTeam1 = 0;
		this.scoreTeam2 = 0;
	}
	
	public void setParent(Rivalry parent) {
		this.parent = parent;
	}
	
	public void setScores(int score1, int score2){
		scoreTeam1 = score1;
		scoreTeam2 = score2;
	}
	
	public Rivalry getParent() {
		return parent;
	}

	public Team getTeam1() {
		return parent.getTeam1();
	}

	public Team getTeam2() {
		return parent.getTeam2();
	}
	
	/** Function getScore
	 * @param team
	 * @return:
	 * 		score of the team passed into function,
	 * 		0 if the team was not one of the two that played
	 */
	public int getScore(Team team){
		if(team.equals(parent.getTeam1())) {
			return scoreTeam1;
		} else if (team.equals(parent.getTeam2())) {
			return scoreTeam2;
		} else {
			return 0;
		}
	}
	
	// NOTICE: these functions return NULL if the score is tied!
	public Team getWinner() {
		if (scoreTeam1 > scoreTeam2) {
			return parent.getTeam1();
		} else if (scoreTeam2 > scoreTeam1) {
			return parent.getTeam2();
		} else {
			return null;
		}
	}
	
	// NOTICE: these functions return NULL if the score is tied!
	public Team getLoser() {
		if (scoreTeam1 < scoreTeam2) {
			return parent.getTeam1();
		} else if (scoreTeam2 < scoreTeam1) {
			return parent.getTeam2();
		} else {
			return null;
		}
	}
	
	// This is simply a pass-through function. Wish there'd be some way to abstract this....
	public Team getOpponent(Team team) {
		return parent.getOpponent(team);
	}
	
//	These two functions return an integer; useful for adding wins/losses. 
	/** Function getLoss
	 * @param team
	 * @return:
	 * 		1 if the team won
	 * 		0 if the team lost
	 * 		0 if the team was not one of the two that played
	 */
	public int getWin(Team team) {
		if (team.equals(parent.getTeam1()) && 
					(getScore(parent.getTeam1()) > getScore(parent.getTeam2()))) {
			return 1;
		} else if (team.equals(parent.getTeam2()) && 
					(getScore(parent.getTeam2()) > getScore(parent.getTeam1()))) {
			return 1;
		} else {
			return 0;
		}
	}
	
	/** Function getLoss
	 * @param team
	 * @return:
	 * 		1 if the team lost
	 * 		0 if the team won
	 * 		0 if the team was not one of the two that played
	 */
	public int getLoss(Team team) {
		if (team.equals(parent.getTeam1()) && 
					(getScore(parent.getTeam1()) < getScore(parent.getTeam2()))) {
			return 1;
		} else if (team.equals(parent.getTeam2()) && 
					(getScore(parent.getTeam2()) < getScore(parent.getTeam1()))) {
			return 1;
		} else {
			return 0;
		}
	}
	
	public int getId() {
		return id;
	}

}
